package com.hmy.contract.service.oss.impl;

import cn.hutool.json.JSONUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.hmy.common.exception.ClientException;
import com.hmy.contract.bo.request.oss.OssFileUploadRequest;
import com.hmy.contract.bo.response.oss.OssFileUploadResponse;
import com.hmy.contract.common.properties.OssProperties;
import com.hmy.contract.service.oss.IOssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author seven.qi 齐胜南
 * @version 1.0
 * @description
 * @date 2023/12/27 13:55
 * aliyun:
 * oss:
 * endpoint
 */

@Service
@Slf4j
public class OssService implements IOssService {

    @Resource
    private OssProperties ossProperties;

    @Override
    public OssFileUploadResponse ossFileUploadByPath(OssFileUploadRequest ossFileUploadRequest) {
        OSS ossClient = null;

        OssFileUploadResponse ossFileUploadResponse = new OssFileUploadResponse();

        try {
            ossClient = new OSSClient(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), ossFileUploadRequest.getStorePath(), new File(ossFileUploadRequest.getFilePath()));

            PutObjectResult result = ossClient.putObject(putObjectRequest);
            log.info("request oss response:{}", JSONUtil.toJsonPrettyStr(result));
            ossFileUploadResponse.setSuccess(Boolean.TRUE);
        } catch (OSSException oe) {
            log.error("Caught an OSSException, which means your request made it to OSS, but was rejected with an error response for some reason.Error Message:{},Error Code:{},Request ID:{},Host ID:{}", oe.getErrorMessage(), oe.getErrorCode(), oe.getRequestId(), oe.getHostId());
            ossFileUploadResponse.setSuccess(Boolean.FALSE);
        } catch (ClientException ce) {
            log.error("Caught an ClientException, which means the client encountered a serious internal problem while trying to communicate with OSS, such as not being able to access the network.Error Message:{}", ce.getMessage(), ce);
            ossFileUploadResponse.setSuccess(Boolean.FALSE);
        } catch (Exception e) {
            ossFileUploadResponse.setSuccess(Boolean.FALSE);
            log.error("get oss signature exception", e);
            throw new ClientException("获取OSS认证异常");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return ossFileUploadResponse;
    }

    @Override
    public OssFileUploadResponse ossFileUploadByStream(OssFileUploadRequest ossFileUploadRequest) {
        OSS ossClient = null;

        OssFileUploadResponse ossFileUploadResponse = new OssFileUploadResponse();

        try {
            ossClient = new OSSClient(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), ossFileUploadRequest.getStorePath(), ossFileUploadRequest.getInputStream());

            PutObjectResult result = ossClient.putObject(putObjectRequest);

            log.info("request oss response:{}", JSONUtil.toJsonPrettyStr(result));
            ossFileUploadResponse.setSuccess(Boolean.TRUE);
            ossFileUploadResponse.setOssPrefix("https://" + ossProperties.getBucketName() + "." + ossProperties.getEndpoint() + "/" + ossFileUploadRequest.getStorePath());
        } catch (OSSException oe) {
            log.error("Caught an OSSException, which means your request made it to OSS, but was rejected with an error response for some reason.Error Message:{},Error Code:{},Request ID:{},Host ID:{}", oe.getErrorMessage(), oe.getErrorCode(), oe.getRequestId(), oe.getHostId());
            ossFileUploadResponse.setSuccess(Boolean.FALSE);
        } catch (ClientException ce) {
            log.error("Caught an ClientException, which means the client encountered a serious internal problem while trying to communicate with OSS, such as not being able to access the network.Error Message:{}", ce.getMessage(), ce);
            ossFileUploadResponse.setSuccess(Boolean.FALSE);
        } catch (Exception e) {
            ossFileUploadResponse.setSuccess(Boolean.FALSE);
            log.error("get oss signature exception", e);
            throw new ClientException("获取OSS认证异常");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return ossFileUploadResponse;
    }
}
